package com.itheima.em.api.controller;

import com.itheima.em.api.vo.FenceParam;
import com.itheima.em.api.vo.PointParam;
import com.itheima.em.enums.FenceType;
import com.itheima.em.enums.PointType;
import com.itheima.em.enums.ProviderType;
import com.itheima.em.pojo.Point;
import com.itheima.em.pojo.TraceFence;
import com.itheima.em.pojo.TraceTerminal;
import com.itheima.em.service.FenceService;
import com.itheima.em.service.PointService;
import com.itheima.em.service.impl.EagleMapServiceFactory;
import com.itheima.em.vo.PageResult;
import com.itheima.em.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标注点相关页面
 *
 * * @author mjj
 * @version 1.0
 * @date 2023/12/1
 */
@Api(tags = "标注点服务")
@RequestMapping("/api/point")
@RestController
public class PointController extends BaseController {

    /**
     * 创建默认标注点
     *
     * @param pointParam
     * @return
     */
    @ApiOperation(value = "创建默认标注点", notes = "默认无分组")
    @PostMapping("default")
    public R<Long> createDefaultPoint(@RequestBody PointParam pointParam) {
        PointService pointService = EagleMapServiceFactory.getService(pointParam.getProvider(), PointService.class);
        return pointService.createPoint(pointParam.getServerId(), pointParam.getName(),
                pointParam.getDesc(), PointType.DEFAULT,0L,
                pointParam.getLatitude(),pointParam.getLatitude(),pointParam.getParam());
    }

    /**
     * 创建围栏标注点
     *
     * @param pointParam
     * @return
     */
    @ApiOperation(value = "创建围栏内标注点", notes = "围栏分组")
    @PostMapping("fence")
    public R<Long> createFencePoint(@RequestBody PointParam pointParam) {
        PointService pointService = EagleMapServiceFactory.getService(pointParam.getProvider(), PointService.class);
        return pointService.createPoint(pointParam.getServerId(), pointParam.getName(),
                pointParam.getDesc(), PointType.FENCE, pointParam.getGroupId(),
                pointParam.getLatitude(),pointParam.getLatitude(),pointParam.getParam());
    }

    //    --------------------------------------------------------------------------------------------

    /**
     * 更新默认标注点
     * @param pointParam
     * @return
     */
    @ApiOperation(value = "更新标注点", notes = "默认")
    @PutMapping("default")
    public R<String> updateDefaultPoint(@RequestBody PointParam pointParam) {
        PointService pointService = EagleMapServiceFactory.getService(pointParam.getProvider(), PointService.class);
        return pointService.updatePoint(pointParam.getServerId(),pointParam.getPointId(), pointParam.getName(),
                pointParam.getDesc(), PointType.DEFAULT,0L,
                pointParam.getLatitude(),pointParam.getLatitude(),pointParam.getParam());
    }

    /**
     * 更新围栏标注点
     *
     * @param pointParam
     * @return
     */
    @ApiOperation(value = "更新围栏标注点", notes = "围栏")
    @PutMapping("fence")
    public R<String> updateFencePoint(@RequestBody PointParam pointParam) {
        PointService pointService = EagleMapServiceFactory.getService(pointParam.getProvider(), PointService.class);
        return pointService.updatePoint(pointParam.getServerId(), pointParam.getPointId(), pointParam.getName(),
                pointParam.getDesc(), PointType.FENCE, pointParam.getGroupId(),
                pointParam.getLatitude(),pointParam.getLatitude(),pointParam.getParam());
    }

    //    --------------------------------------------------------------------------------------------

    /**
     * 删除标注点
     *
     * @param pointParam
     * @return
     */
    @ApiOperation(value = "删除标注点", notes = "必须参数：serverId、pointIds")
    @DeleteMapping
    public R<String> deletePoint(@RequestBody PointParam pointParam) {
        PointService pointService = EagleMapServiceFactory.getService(pointParam.getProvider(), PointService.class);
        return pointService.deletePoint(pointParam.getServerId(), pointParam.getPointIds());
    }


    //    --------------------------------------------------------------------------------------------
    /**
     * 分页查询标注点列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "provider", value = "服务提供商，必须大写，如：BAIDU,AMAP,NONE，默认：高德地图", required = true),
            @ApiImplicitParam(name = "page", value = "页数，默认：1"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认：20")})
    @ApiOperation(value = "查询标注点列表", notes = "查询标注点列表")
    @GetMapping("list")
    public R<PageResult<Point>> queryPointList(

            @RequestParam(value = "pointType", defaultValue = "DEFAULT") PointType pointType,
            @RequestParam(value = "groupId",defaultValue = "0") Long groupId,
            @RequestParam(value = "provider", defaultValue = "NONE") ProviderType provider,
            @RequestParam(value = "serverId") Long serverId,
                                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        PointService pointService = EagleMapServiceFactory.getService(provider, PointService.class);
        return pointService.queryPointList(serverId,pointType,groupId,page, pageSize);
    }

    /**
     * 查询标记点信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "provider", value = "服务提供商，必须大写，如：BAIDU,AMAP,NONE，默认：高德地图", required = true),
            @ApiImplicitParam(name = "serverId", value = "服务id", required = true),
            @ApiImplicitParam(name = "pointId", value = "标记点id", required = true)})
    @ApiOperation(value = "查询标记点信息", notes = "必须参数：serverId、pointId")
    @GetMapping
    public R<Point> queryByPointId(@RequestParam(value = "provider", defaultValue = "NONE") ProviderType provider,
                                        @RequestParam(value = "serverId") Long serverId,
                                        @RequestParam(value = "pointId") Long pointId) {
        PointService pointService = EagleMapServiceFactory.getService(provider, PointService.class);
        return pointService.queryByPointId(serverId, pointId);
    }

}
